<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
<!--    <el-form-item label="创建时间" prop="gmtCreate">-->
<!--      <el-input v-model="dataForm.gmtCreate" placeholder=""></el-input>-->
<!--    </el-form-item>-->
<!--    <el-form-item label="修改时间" prop="gmtModified">-->
<!--      <el-input v-model="dataForm.gmtModified" placeholder=""></el-input>-->
<!--    </el-form-item>-->
<!--    <el-form-item label="code" prop="templateCode">-->
<!--      <el-input v-model="dataForm.templateCode" placeholder=""></el-input>-->
<!--    </el-form-item>-->
    <el-form-item label="模板名" prop="templateName">
      <el-input v-model="dataForm.templateName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="提示词" prop="templatePromptWord">
      <el-input v-model="dataForm.templatePromptWord" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="触发器" prop="templateTriggerWord">
      <el-input v-model="dataForm.templateTriggerWord" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="适用场景" prop="templateApplicableScene">
      <el-input v-model="dataForm.templateApplicableScene" placeholder=""></el-input>
    </el-form-item>
<!--    <el-form-item label="扩展" prop="extInfo">-->
<!--      <el-input v-model="dataForm.extInfo" placeholder=""></el-input>-->
<!--    </el-form-item>-->
<!--    <el-form-item label="环境" prop="env">-->
<!--      <el-input v-model="dataForm.env" placeholder=""></el-input>-->
<!--    </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          gmtCreate: '',
          gmtModified: '',
          templateCode: '',
          templateName: '',
          templatePromptWord: '',
          templateTriggerWord: '',
          templateApplicableScene: '',
          extInfo: '',
          env: ''
        },
        dataRule: {
          // gmtCreate: [
          //   { required: true, message: '不能为空', trigger: 'blur' }
          // ],
          // gmtModified: [
          //   { required: true, message: '不能为空', trigger: 'blur' }
          // ],
          // templateCode: [
          //   { required: true, message: '不能为空', trigger: 'blur' }
          // ],
          templateName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          templatePromptWord: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          templateTriggerWord: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          templateApplicableScene: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          // extInfo: [
          //   { required: true, message: '不能为空', trigger: 'blur' }
          // ],
          // env: [
          //   { required: true, message: '不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/template/template/queryWormholeGalaxyTemplate`),
              method: 'post',
              data: this.$http.adornData({
                "id":id
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.gmtCreate = data.wmhGalaxyTemplate.gmtCreate
                this.dataForm.gmtModified = data.wmhGalaxyTemplate.gmtModified
                this.dataForm.templateCode = data.wmhGalaxyTemplate.templateCode
                this.dataForm.templateName = data.wmhGalaxyTemplate.templateName
                this.dataForm.templatePromptWord = data.wmhGalaxyTemplate.templatePromptWord
                this.dataForm.templateTriggerWord = data.wmhGalaxyTemplate.templateTriggerWord
                this.dataForm.templateApplicableScene = data.wmhGalaxyTemplate.templateApplicableScene
                this.dataForm.extInfo = data.wmhGalaxyTemplate.extInfo
                this.dataForm.env = data.wmhGalaxyTemplate.env
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/template/template/${!this.dataForm.id ? 'createWormholeGalaxyTemplate' : 'createWormholeGalaxyTemplate'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'gmtCreate': this.dataForm.gmtCreate,
                'gmtModified': this.dataForm.gmtModified,
                'templateCode': this.dataForm.templateCode,
                'templateName': this.dataForm.templateName,
                'templatePromptWord': this.dataForm.templatePromptWord,
                'templateTriggerWord': this.dataForm.templateTriggerWord,
                'templateApplicableScene': this.dataForm.templateApplicableScene,
                'extInfo': this.dataForm.extInfo,
                'env': this.dataForm.env
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
